package com.jt.tenant.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.CancelOrder;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by 马长鸣 on 2016/5/14.
 */
public class OilQrcodeActivity extends JTActivity implements View.OnClickListener {

    private TextView title,name,phone,address;

    private ImageView logo,oil_orcode;

    private LinearLayout lin_navi;

    private Bitmap qrcode;

    private String order_id,gs_name,gs_phone,gs_address,lon,lat,gs_logo;

    private double mLat, mLon;

    private CancelOrder cancelOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_oil_qrcode);

        order_id = getIntent().getStringExtra("order_id");

        gs_name = getIntent().getStringExtra("gs_name");

        gs_phone = getIntent().getStringExtra("gs_phone");

        gs_address = getIntent().getStringExtra("gs_address");

        lon = getIntent().getStringExtra("lon");

        lat = getIntent().getStringExtra("lat");

        gs_logo = getIntent().getStringExtra("gs_logo");

        initView();
    }

    private void initView(){

        (title = (TextView) findViewById(R.id.tv_title_name)).setText("加油二维码");

        logo = (ImageView)findViewById(R.id.iv_oil_qrcode_logo);

        name = (TextView) findViewById(R.id.tv_oil_qrcode_name);//订单加油站名

        phone = (TextView)findViewById(R.id.tv_oil_qrcode_phone);

        address = (TextView)findViewById(R.id.tv_oil_qrcode_address);//订单加油站地址

        (lin_navi = (LinearLayout)findViewById(R.id.lin_oil_qrcode_navi)).setOnClickListener(this);//导航

        oil_orcode = (ImageView)findViewById(R.id.oil_qrcode);//订单二维码

        //从七牛获取图片
        JTApplication.UtilAsyncBitmap.get(gs_logo, logo, R.mipmap.loading);

        name.setText(gs_name);

        phone.setText(gs_phone);

        address.setText("地址："+gs_address);

        qrcode = generateQRCode(order_id);

        oil_orcode.setImageBitmap(qrcode);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.oil_refund:

                cancelOrder = new CancelOrder(new AsyCallBack<CancelOrder.Info>(){
                    @Override
                    public void onSuccess(String toast, int type, CancelOrder.Info info) throws Exception {
                        //super.onSuccess(toast, type, info);

                        if(info.msg.equals("000")){

                            finish();

                            UtilToast.show(context,"订单已取消");


                        }
                    }
                });
                break;

            case R.id.lin_oil_qrcode_navi:

                startActivity(new Intent(context, BasicNaviActivity.class)

                        .putExtra("mLat", Double.parseDouble(JTApplication.lat))

                        .putExtra("mLon", Double.parseDouble(JTApplication.lon))

                        .putExtra("goLat",Double.parseDouble(lat))

                        .putExtra("goLon",Double.parseDouble(lon)));

                break;

            default:
                break;
        }

    }

    private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {

        BitMatrix matrix1 = updateBitMatrix(matrix,12);

        int w = matrix1.getWidth();

        int h = matrix1.getHeight();

        int[] rawData = new int[w * h];

        for (int i = 0; i < w; i++) {

            for (int j = 0; j < h; j++) {

                int color = Color.WHITE;

                if (matrix1.get(i, j)) {

                    color = Color.BLACK;
                }

                rawData[i + (j * w)] = color;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);

        bitmap.setPixels(rawData, 0, w, 0, 0, w, h);

        return bitmap;
    }

    private Bitmap generateQRCode(String content) {

        try {
            QRCodeWriter writer = new QRCodeWriter();

            // MultiFormatWriter writer = new MultiFormatWriter();

            BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 278, 278);

            return bitMatrix2Bitmap(matrix);

        } catch (WriterException e) {

            e.printStackTrace();

        }

        return null;
    }
    private BitMatrix updateBitMatrix(BitMatrix matrix, int margin){

        int tempM = margin*2;

        int[] rec = matrix.getEnclosingRectangle();   //获取二维码图案的属性

        int resWidth = rec[2] + tempM;

        int resHeight = rec[3] + tempM;

        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定义边框生成新的BitMatrix

        resMatrix.clear();

        for(int i= margin; i < resWidth- margin; i++){   //循环，将二维码图案绘制到新的bitMatrix中

            for(int j=margin; j < resHeight-margin; j++){

                if(matrix.get(i-margin + rec[0], j-margin + rec[1])){

                    resMatrix.set(i,j);

                }

            }

        }

        return resMatrix;

    }

//    private SpannableString orderid(String str){
//
//        SpannableString s = new SpannableString("订单编号："+str);
//
//        s.setSpan(new ForegroundColorSpan(Color.parseColor("#3c3c3c")),0, 5, 0);
//
//        s.setSpan(new ForegroundColorSpan(Color.parseColor("@color/MainColor")),5, s.length(), 0);
//
//        return s;
//
//    }







}
